# My first hello work python
import os
import math
from datetime import datetime
import re

filename = os.environ.get('PYTHONSTARTUP')
if filename and os.path.isfile(filename) :
    execfile(filename)
    
print "Hello World\n"

# illustration of numbers
# multiple assignment to variables is possible

x = y = z = 0
print x
print y
print z

# illustration of complex numbers

cmp = 3 + 4j
print cmp.real
print cmp.imag
print abs(cmp)

cmp1 = complex(2, 3)
print cmp1.real
print cmp1.imag
print abs(cmp1)
print math.sqrt(cmp1.real**2 + cmp1.imag**2)

# conversion of string into a number
a = '2'
b = '3'

cmp2 = complex(int(a), int(b))
print cmp2.real, cmp2.imag, abs(cmp2)

# string assignment 
hello = "This is rather long string containing \n\
several lines of text just as you would do in C.\n\
    Note that a white space at the beginning of the line is\
 significant."

print hello

# raw string assignment
hello =r"This is a rather long string containing\n\
several lines of text much as you would do in C."

print hello

# string in triple quote is magical
print """
Usage: thingy [OPEIONS]
    -h            Display this usage message
    -H hostname    Hostname to connect to
"""

# string concatenation or glued with + and repeated in *
word = 'Help'+'A'
print word
print '<'+word*5+'>'

# string concatenation by placing them next to each other
print 'str' 'ing'

word = 'Help' 'A'

#slice notation for indexing string
print word[4]
print word[0:2]
print word[2:4]

# default of slice notation. If first argument is omitted it defaults to 0. If second omitted it is length of the string
print word[:2]
print word[2:]

# python strings are immutable

try:
    word[0] = ''
except Exception:
    print "String in Python is immutable"
        
#dgenerated slice indices are handled gracefully
print word[1:100]

#indices could be negative number, it means they count from the right
print word[-1]
#out of range slice indices are truncated
print word[-100:]

#unicoding string
print u'Hello \u0020World !'

#lets play with lists here

a = ['spam', 'eggs', 100, 1234]
print a
print len(a)

# lists can be sliced, concatenated, indexed and so on
print a[0]
print a[3]
print a[-2]
print a[1:-1]
print a[:2] + ['becon', 2*2]
print 3*a[:3] + ['Boe!']

#list are mutable
print a
a[2] += 23
print a

# replace some items:
a[0:2] = [1, 12]
print a
# remove some
a[0:2] = []
print a
# insert some: pay attention to slice notation 1:1
a[1:1] = ['bletch', 'xyzzy']
print a
a[:0] = a # Insert (a copy of) itself at the beginning
print a
print len(a)

#nested list
q = [2,3]
p = [1, q, 4]
print len(p)
print p
print p[1][0]
p[1].append('xtra')
print p
print q

# Fibonacci series:
# the sum of two elements defines the next
a, b = 0, 1
while b < 100:
    print b,
    a, b = b, a+b

x = int(raw_input("Please enter an integer: "))
if x < 0:
    x = 0
    print 'Negative changed to zero'
elif x == 0:
    print 'Zero'
elif x == 1:
    print 'Single'
else:
    print 'More'
    
# Measure some strings:
a = ['cat', 'window', 'defenestrate']
for x in a:
    print x, len(x)

#range function
a = ['Mary', 'had', 'a', 'little', 'lamb']
for i in range(len(a)):
    print i, a[i]

# interesting else clause in Loops
for n in range(2,10):
    for x in range(2,n):
        if n % x == 0:
            print n, 'equals', x, '*', n/x
            break
        else:
        # loop fell through without finding a factor
            print n, 'is a prime number'
            
# pass Statement does nothing except to fill space of syntactic requirement

def fib(n): #write Fibnocci series up to no
    """Print a Fibnocci series up to n. """
    a,b = 0,1
    while b < n:
        print b,
        a,b = b, a+b
print "Writing Fibnocci series up to 2000"
time1 = datetime.now()
fib (2000) 
time2 = datetime.now()
print 'time to calculate fibnocci', (time2 - time1).microseconds


test = "{executed in 511 msec}"
parts = re.split("({executed in )()( msec})", test)
